package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.GoodsCommentDao;
import com.baichang.taoshunda.entity.GoodsComment;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class GoodsCommentDaoImpl extends BaseDaoImpl<GoodsComment> implements GoodsCommentDao {
    /**
     * 查询每个商家的用户评论
     * 接口
     * 武圣路
     *
     * @param bussId   商家编号
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public List<Map<String, Object>> getCommentByBussId(int bussId, int nowPage, int pageSize) {
        String sql = "SELECT u.head_pic AS headPic, u.account,u.nick_name nickName, gc.id, gc.goods_id AS goodsId, gc.user_id AS userId , gc.content, gc.images, gc.created, gc.spec_json, gc.star_num , gc.bus_id AS busId, gc.order_number FROM t_goods_comment gc INNER JOIN t_user u ON u.id = gc.user_id WHERE goods_id=?";
        sql += "ORDER by gc.id DESC";
        return listForMapList(sql, nowPage, pageSize, bussId);
    }

    @Override
    public Map<String, Object> getSartNum(int bussId) {
        String sql = "SELECT COUNT(1) people,AVG(star_num) starNum FROM t_goods_comment WHERE goods_id=?";
        return queryForMap(sql, bussId);
    }

    @Override
    public int addComment(int orderId, int userId, int score, String content, String picture, String spec, int bussId, Integer goodsId) {
        String sql = "INSERT INTO `t_goods_comment` (`user_id`, `content`, `images`, `spec_json`, `star_num` , `bus_id`, `order_id`) VALUES (?,?, ?, ?, ? , ?, ?)";
        return update(sql, userId, content, picture, spec, score, bussId, orderId);
    }

    /**
     * 查询订单评论
     *
     * @param orderId
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryOrderComment(Integer orderId, int nowPage, int pageSize) {
        String sql = "SELECT gc.*,tu.nick_name AS nickName,tb.buss_name AS busName, " +
                " IFNULL(gc.review, '') AS review , IFNULL(gc.content_reply, '') AS contentReply , " +
                "IFNULL(gc.review_time, '') AS reviewTime , IFNULL(gc.content_reply_time, '') AS contentReplyTime ," +
                " IFNULL(gc.review_img, '') AS reviewImg , IFNULL(gc.review_reply, '') AS reviewReply ," +
                " IFNULL(gc.review_reply_time, '') AS reviewReplyTime  FROM t_goods_comment gc " +
                " LEFT JOIN t_user tu ON gc.user_id=tu.id LEFT JOIN t_bussiness tb ON tb.id=gc.bus_id WHERE order_id=?";
        sql += "ORDER by gc.id DESC";
        return pageForMapList(sql, nowPage, pageSize, orderId);
    }

    /**
     * 查看商家评论列表
     *
     * @param bussId   商家编号
     * @param nowPage  当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public List<Map<String, Object>> findCommentList(Integer bussId, Integer nowPage, Integer pageSize) {
        String sql = " SELECT gc.*,gc.bus_id busId,us.head_pic headPic,us.nick_name nickName ," +
                " IFNULL(gc.review, '') AS review , IFNULL(gc.content_reply, '') AS contentReply , " +
                "IFNULL(gc.review_time, '') AS reviewTime , IFNULL(gc.content_reply_time, '') AS contentReplyTime ," +
                " IFNULL(gc.review_img, '') AS reviewImg , IFNULL(gc.review_reply, '') AS reviewReply ," +
                " IFNULL(gc.review_reply_time, '') AS reviewReplyTime  FROM t_goods_comment gc " +
                "INNER JOIN t_user us ON gc.user_id=us.id WHERE gc.goods_id=? ORDER BY gc.created DESC ";
        return listForMapList(sql, nowPage, pageSize, bussId);
    }

    @Override
    public int getTotal(int bussId) {
        String sql = " SELECT count(0) FROM t_goods_comment gc INNER JOIN t_user us ON gc.user_id=us.id WHERE gc.bus_id=? ORDER BY gc.created DESC ";
        return queryForInt(sql, bussId);
    }

    @Override
    public List<Map<String, Object>> findCommentByUserId(Integer userId, Integer nowPage, Integer pageSize) {
        String sql = "SELECT gc.id, gc.goods_id, gc.user_id, gc.content, gc.images, gc.created, gc.spec_json, gc.star_num," +
                " gc.bus_id, gc.order_number  ,IFNULL(gc.review, '') AS review , IFNULL(gc.content_reply, '') AS contentReply , " +
                "IFNULL(gc.review_time, '') AS reviewTime , IFNULL(gc.content_reply_time, '') AS contentReplyTime ," +
                " IFNULL(gc.review_img, '') AS reviewImg , IFNULL(gc.review_reply, '') AS reviewReply , " +
                "IFNULL(gc.review_reply_time, '') AS reviewReplyTime , g.head_pic goodsPic, " +
                "g.`name` goodsName, g.unit FROM t_goods_comment AS gc INNER JOIN t_goods g on g.id=gc.goods_id" +
                " WHERE gc.user_id=? ORDER BY gc.id DESC ";
        return listForMapList(sql, nowPage, pageSize, userId);
    }

    @Override
    public boolean review(Integer commentId, String review, String reviewImg) {
        String sql = "UPDATE `t_goods_comment` SET `review`=?,`review_img`=?, `review_time`=NOW() WHERE id=? ";
        return update(sql, review, reviewImg, commentId) > 0;
    }

    @Override
    public boolean commentReply(Integer commentId, String commentReply) {
        String sql = "UPDATE `t_goods_comment` SET `content_reply`=?, `content_reply_time`=NOW() WHERE id=? ";
        return update(sql, commentReply, commentId) > 0;
    }

    @Override
    public boolean reviewReply(Integer commentId, String reviewReply) {
        String sql = "UPDATE `t_goods_comment` SET `review_reply`=?, `review_reply_time`=NOW() WHERE id=? ";
        return update(sql, reviewReply, commentId) > 0;
    }
}